<template>
  <el-dialog
    :visible.sync="visible"
    :title="!dataForm.id ? $t('add') : $t('update')"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'"
    >
      <el-form-item
        label="名称"
        prop="name"
      >
        <el-input
          v-model="dataForm.name"
          placeholder="名称"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="状态"
        prop="status"
      >
        <!-- <el-input
          v-model="dataForm.status"
          placeholder="状态  0：停用   1：正常"
        ></el-input> -->
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="0">停用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="站点"
        prop="status"
      >
        <el-tag
          :key="index"
          v-for="(item,index) in TagList"
          size='medium'
          closable
          :disable-transitions="true"
          @close="handleClose(item)"
          @click="handleClick(item)"
          style="margin-right:10px;"
        >
          {{item.name}}
        </el-tag>
        <el-input
          class="input-new-tag"
          v-if="inputVisible"
          v-model="inputValue"
          ref="saveTagInput"
          size='mini'
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
          style="width:80px"
        >
        </el-input>
        <el-button
          v-else
          class="button-new-tag"
          @click="showInput"
          size='mini'
          style="width:80px"
        >添加</el-button>
      </el-form-item>

    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button
        type="primary"
        @click="dataFormSubmitHandle()"
      >{{ $t('confirm') }}</el-button>
    </template>

    <el-dialog
      :visible.sync="UpdateVisible"
      :modal='false'
      title="修改站点"
      width='20%'
    >
      <el-form
        :model="UpdateForm"
        ref="UpdateForm"
        label-width="50px"
        label-position='top'
      >
        <el-form-item
          label="站点名称"
          prop="name"
        >
          <el-input
            v-model="UpdateForm.name"
            placeholder="站点名称"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="站点序号"
          prop="name"
        >
          <el-input
            v-model="UpdateForm.sort"
            placeholder="站点序号"
          ></el-input>
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button @click="UpdateVisible = false">取消</el-button>
        <el-button
          type="primary"
          @click="UpdateFormFun"
        >修改</el-button>
      </template>
    </el-dialog>

  </el-dialog>

</template>

<script>
import debounce from "lodash/debounce";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: "",
        name: "",
      },
      inputVisible: false,
      inputValue: "",
      TagList: [],
      UpdateVisible: false,
      UpdateForm: {
        id: "",
        name: "",
        sort: ""
      }
    };
  },
  computed: {
    dataRule() {
      return {
        name: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur"
          }
        ],
      };
    }
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
          this.metroStationList();
        } else {
          this.TagList = [];
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/dictionary/metro/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data
          };
        })
        .catch(() => {});
    },
    //获取站点
    metroStationList() {
      let _this = this;
      // _this.TagList=[];
      this.$http
        .get(`/dictionary/metrostation/metroStationList`, {
          params: {
            id: this.dataForm.id
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          _this.TagList = res.data;
        })
        .catch(() => {});
    },

    //删除站点
    handleClose(tag) {
      let arr = [];
      arr.push(tag.id);
      this.$http
        .delete(`/dictionary/metrostation`, {
          data: arr
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.metroStationList();
          this.TagList.splice(this.TagList.indexOf(tag), 1);
        })
        .catch(() => {});
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    //点击
    handleClick(item) {
      this.UpdateVisible = true;
      this.UpdateForm = item;
    },

    //修改站点
    UpdateFormFun() {
      let _this = this;
      this.$http
        .put(`/dictionary/metrostation`, this.UpdateForm)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message.success("更改成功");
          this.UpdateVisible = false;
        })
        .catch(() => {});
    },

    //添加站点
    handleInputConfirm() {
      let inputValue = this.inputValue;
      let _this = this;
      if (inputValue) {
        // this.TagList.push(inputValue);
        this.$http
          .post(`/dictionary/metrostation`, {
            metroId: _this.dataForm.id,
            name: inputValue
          })
          .then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg);
            }
            this.metroStationList();
            this.$message.success("添加站点成功");
          })
          .catch(() => {});
      }
      this.inputVisible = false;
      this.inputValue = "";
    },

    // 表单提交
    dataFormSubmitHandle: debounce(
      function() {
        this.$refs["dataForm"].validate(valid => {
          if (!valid) {
            return false;
          }
          this.$http[!this.dataForm.id ? "post" : "put"](
            "/dictionary/metro/",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    )
  }
};
</script>
